package sjd.ahsj.springbootmybatisplusoracle.mapper;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import sjd.ahsj.springbootmybatisplusoracle.entity.IFactory;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author zhanghaogao
 * @since 2018-10-18
 */
public interface IFactoryMapper extends BaseMapper<IFactory> {


    @Insert("INSERT INTO ${tableName} (${keys}) VALUES (${values})")
    Integer insertHistoryInfo(@Param("tableName") String tableName, @Param("keys") String keys,
                              @Param("values") String values);

    /**
     * 创建实时表
     *
     * @param tableName
     * @param sql
     * @return
     */
    @Update("CREATE TABLE ${tableName}  (`id` bigint(20) NOT NULL AUTO_INCREMENT,`create_date` datetime NULL," +
            "${sql} PRIMARY KEY (`id`))")
    Integer createCurrent(@Param("tableName") String tableName, @Param("sql") String sql);


    /**
     * 创建历史表
     *
     * @param tableName
     * @param sql
     * @return
     */
    @Update("CREATE TABLE ${tableName}  (`id` integer(11) NOT NULL AUTO_INCREMENT,`create_date` datetime NULL," +
            "${sql} PRIMARY KEY (`id`))")
    Integer createHistory(@Param("tableName") String tableName, @Param("sql") String sql);


    /**
     * 获取某机主信息
     *
     * @param tableName
     * @return
     */
    @Select("select * from ${tableName}")
    List<Map<String, Object>> selectFactoryInfo(@Param("tableName") String tableName);

    /**
     * 获取某机主信息
     *
     * @param tableName
     * @return
     */
    @Select("select ${sql} from ${tableName}")
    List<Map<String, Object>> selectFactoryInfoBySql(@Param("tableName") String tableName, @Param("sql") String sql);


    /**
     * 获取电站历史数据
     *
     * @param tableName
     * @return
     */
    @Select("select * from ${tableName} where create_date>#{stDate} and create_date<#{enDate}")
    List<Map<String, Object>> selectFactoryInfoHistory(@Param("tableName") String tableName,
                                                       @Param("stDate") Date stDate,
                                                       @Param("enDate") Date enDate);


    /**
     * 查询绘图表平均值
     *
     * @param tableName
     * @param sql
     * @param stDate
     * @param enDate
     * @return
     */
    @Select("select ${sql} from ${tableName} where create_date>=#{stDate} and create_date<=#{enDate} and type=#{type}")
    List<Map<String, Object>> selectPlotInfoBySql(@Param("tableName") String tableName, @Param("sql") String sql,
                                                  @Param("stDate") Date stDate, @Param("enDate") Date enDate, @Param(
            "type") Integer type);




    @Select("select * from ${tableName} where type=#{type} ORDER BY create_date desc LIMIT 40")
    List<Map<String, Object>> selectPlotInfoByType(@Param("tableName") String tableName, @Param("type") Integer type);
    /**
     * 删除历史数据
     *
     * @param tableName
     * @param enDate
     * @param type
     * @return
     */
    @Delete("delete from ${tableName} where create_date<#{enDate} and type=#{type}")
    Integer delPlotInfoByType(@Param("tableName") String tableName, @Param("enDate") Date enDate,
                              @Param("type") Integer type);
}
